package com.example.reference_book.controller;

import com.example.reference_book.entity.CourseBook;
import com.example.reference_book.entity.CourseBookInfo;
import com.example.reference_book.service.CourseBookService;
import com.example.reference_book.util.CourseBookSearchUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程书籍表(CourseBook)表控制层
 *
 * @author makejava
 * @since 2022-06-07 22:19:03
 */
@RestController
@RequestMapping("courseBook")
public class CourseBookController {
    /**
     * 服务对象
     */
    @Resource
    private CourseBookService courseBookService;
    private CourseBookSearchUtil searchUtil = CourseBookSearchUtil.getSearchUtil();
    /**
     * 分页查询
     *
     * @param courseBook 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<CourseBook>> queryByPage(CourseBook courseBook, PageRequest pageRequest) {
        return ResponseEntity.ok(this.courseBookService.queryByPage(courseBook, pageRequest));
    }

    @GetMapping("/searchByKey/{type}/{key}")
    public ResponseEntity<List<CourseBookInfo>> searchByKey(@PathVariable("type") Integer type, @PathVariable("key") String key){
        return ResponseEntity.ok(this.searchUtil.searchByKey(type,key));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public ResponseEntity<CourseBook> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.courseBookService.queryById(id));
    }

    /**
     * 查询所有数据
     */
    @PostMapping("/selectAll")
    public ResponseEntity<List<CourseBookInfo>> selectAll() {
        List<CourseBookInfo> list = this.courseBookService.selectAll();
        searchUtil.init(list);
        return ResponseEntity.ok(list);
    }

    /**
     * 新增数据
     *
     * @param courseBook 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseEntity<CourseBook> add(CourseBook courseBook) {
        return ResponseEntity.ok(this.courseBookService.insert(courseBook));
    }

    /**
     * 编辑数据
     *
     * @param courseBook 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public ResponseEntity<CourseBook> edit(CourseBook courseBook) {
        return ResponseEntity.ok(this.courseBookService.update(courseBook));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.courseBookService.deleteById(id));
    }

    @PostMapping("/deleteAll")
    public ResponseEntity<Boolean> deleteAllById(int[] ids) {
        for(int i=0;i<ids.length;i++){
            this.courseBookService.deleteById(ids[i]);
        }
        return ResponseEntity.ok(true);
    }

}

